<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增数据库信息')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-sbrDatabase-add">
        <input type="hidden" name="validFlag" id="validFlag">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">数据库名称：</label>
            <div class="col-sm-4">
                <input name="databaseName" class="form-control" type="text" required>
            </div>
            <label class="col-sm-2 control-label is-required">数据库版本：</label>
            <div class="col-sm-4">
                <select name="driverType" class="form-control m-b" th:with="type=${@dict.getType('sbr_driver_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">数据库地址：</label>
            <div class="col-sm-4">
                <input name="host" class="form-control" type="text" required>
            </div>
            <label class="col-sm-2 control-label is-required">端口号：</label>
            <div class="col-sm-4">
                <input name="port" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">用户名：</label>
            <div class="col-sm-4">
                <input name="userName" class="form-control" type="text">
            </div>
            <label class="col-sm-2 control-label is-required">密码：</label>
            <div class="col-sm-4">
                <input name="password" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"></label>
            <div class="col-sm-4">
                <a class="btn btn-primary single" onclick="testConn()">
                    <i class="fa fa-edit"></i> 测试连接
                </a>
            </div>
            <label class="col-sm-2 control-label"></label>
            <div class="col-sm-4" id="testConnMsg">
                <span class="badge">未测试</span>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "sqlbuilder/sbrDatabase"
    $("#form-sbrDatabase-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var validFlag = $("#validFlag").val();
            if (validFlag == '') {
                $.modal.alertError('未测试数据连接！');
                return;
            }
            if (validFlag == '0') {
                $.modal.confirm("当前数据库连接未测试成功,保存后无法正常使用?", function() {
                    $.operate.save(prefix + "/add", $('#form-sbrDatabase-add').serialize());
                });
            } else {
                $.operate.save(prefix + "/add", $('#form-sbrDatabase-add').serialize());
            }
        }
    }
    function testConn() {
        $.modal.loading("数据库测试连接中...");
        $.ajax({
            type: "post",
            url: prefix + "/testConn",
            data: $('#form-sbrDatabase-add').serialize(),
            success: function(data) {
                if (data.code == 0) {
                    $.modal.msgSuccess('连接成功!');
                    $("#validFlag").val(1);
                    $("#testConnMsg").html('<span class="badge badge-success">连接成功</span>');
                } else {
                    $.modal.msgError('连接失败, 请检查!');
                    $("#validFlag").val(0);
                    $("#testConnMsg").html('<span class="badge label-warning">连接失败</span>');
                }
                $.modal.closeLoading();
            }
        });
    }

</script>
</body>
</html>